package br.moneymonitor.database;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class DataBaseFactory{

	public static final String DATABASE_NAME = "MoneyMonitor.db";
	public static final int VERSION = 3;
	protected DataBaseHelper  dbHelper;
	protected static SQLiteDatabase db;
	
	public DataBaseFactory(Context context) {
		//Cria o bando de dados
		db = context.openOrCreateDatabase(DATABASE_NAME, Context.MODE_PRIVATE, null);
		//Cria as tabelas do banco
		dbHelper = new DataBaseHelper(context, DATABASE_NAME, VERSION);
		//Abre para leitura
		db = dbHelper.getWritableDatabase();
	}

	public void close(){
		close();
	}
	
	protected long insert(String table, ContentValues values){
		long id = db.insert(table, "", values);		
		return id;
	}
	
	protected long update(String table, ContentValues values, String whereClause, String[] whereArgs){
		long id = db.update(table, values, whereClause, whereArgs);
		return id;
	}
	
	protected Cursor select(String table, String[] columns, String selection, String[] selectionArgs){
		return select(table, columns, selection, selectionArgs, null, null, null);
	}
	
	protected Cursor select(String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy){
		Cursor c = db.query(table, columns, selection, selectionArgs, groupBy, having, orderBy);
		return c;
	}
	
}
